package com.ifengchao.util.zzy;

import com.ifengchao.commonUtil.JsonUtil;
import net.sf.json.JSONObject;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Zhi Zi yun test.
 *
 * @Author YiMing
 * @Description: 调用智子云接口测试
 * @Date: Created in 上午9:54 2017/8/9
 * @Modified By
 */
public class ZhiZiYunUtil {

	private static final Logger logger = LogManager.getLogger(ZhiZiYunUtil.class);

	private static String agentId;	//代理商编号

	private static String key; //key4token密钥

	private static String zzyUri; // uri: https://test.zhiziyun.com/api-service/api/

	/**
	 * Gets zhi zi yun data.
	 *
	 * @param uri  the uri 地址
	 * @param paramMap the paramMap 传参
	 * @return the zhi zi yun data
	 * @throws Exception the exception
	 */
	public static JSONObject getZhiZiYunData (String uri, Map paramMap) throws Exception {

		//获取配置文件中的数据
//		agentId = PropertiesUtil.getString("zhiZiYun.data");
//		key = PropertiesUtil.getString("zhiZiYun.key");
//		zzyUri = PropertiesUtil.getString("zhiZiYun.uri");

		String agentId = "354";//代理商编号 354.
		String key = "9c45dc0b-e81b-4b93-9b68-73ac3e893fef";//key4token:9c45dc0b-e81b-4b93-9b68-73ac3e893fef
		String zzyUri = "http://test.zhiziyun.com/api-service/api/";

		List<BasicNameValuePair> params=new ArrayList<>();
		String json = "";
		String data = agentId + System.currentTimeMillis(); //代理商编号 + 系统当前时间通过key4token密钥DES加密生成token

		//判断是否传入参数
		if (paramMap != null && paramMap.size() > 0) {
			json = JsonUtil.getJsonStringFromObject(paramMap);
			params.add(new BasicNameValuePair("json", json));
		}
		logger.info("调用智子云api--data:"+data+";paramMap:"+json);
		System.out.println("调用智子云api--data:"+data+";paramMap:"+json);
		//生成token
		String token = DESCoder.encrypt(data, key);
		logger.info("调用智子云api--token："+token);
		CloseableHttpClient client = HttpClients.createDefault();

		//根据URL生成post对象,并添加参数
		//HttpPost post = new HttpPost("http://test.zhiziyun.com/api-service/api/" + uri);
		HttpPost post = new HttpPost(zzyUri + uri);
		params.add(new BasicNameValuePair("token",token));
		params.add(new BasicNameValuePair("agentid",agentId));

		post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		CloseableHttpResponse resp = null;
		try {
			resp=client.execute(post);
		}catch (Exception e){
			logger.error("连接智子云服务器异常：",e);
			throw new Exception("网络与服务器连接异常！");
		}

		if (resp.getStatusLine().getStatusCode() == 200) {
			org.apache.http.HttpEntity entity = resp.getEntity();
			String message = EntityUtils.toString(entity, "utf-8");
			return JSONObject.fromObject(message);
		} else {
			System.out.println("请求失败");
			return null;
		}
	}

	public static void main(String[] args) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String[] arr = new String[]{"sgpu100Cav6"};
		map.put("planids",arr);
		map.put("starttime","2017-06-30");
		map.put("endtime","2017-07-31");
//		map.put("advertiserLiteName", "蜂巢测试1有限公司");
//		map.put("advertiserName", "蜂巢测试1有限公司");
//		map.put("domain", "https://www.lincomb.com");
//		map.put("brandImgUrl", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502865625083&di=61ebcea200e8b4e34908f71966bfd6da&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0145d356e0e96732f875520facd733.jpg");
//		map.put("brandName", "蜂巢测试1有限公司");
//		map.put("csfServiceFee", "0.3");
		System.out.println(getZhiZiYunData("rtbreport/getBySite",map));
	}
}
